import { FindAllCarInfoDto } from './dto/findAll-car_info.dto';
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
} from '@nestjs/common';
import { ApiOperation } from '@nestjs/swagger';
import { ApiResult } from 'src/config';
import { CarInfoService } from './car_info.service';
import { CreateCarInfoDto } from './dto/create-car_info.dto';
import { UpdateCarInfoDto } from './dto/update-car_info.dto';

@Controller('car_info')
export class CarInfoController {
  constructor(private readonly carInfoService: CarInfoService) {}

  @ApiOperation({ summary: '创建汽车信息' })
  @Post()
  async create(@Body() createCarInfoDto: CreateCarInfoDto): Promise<ApiResult> {
    const result = await this.carInfoService.create(createCarInfoDto);
    return ApiResult.SUCCESS(result, 0, '创建成功!');
  }

  @ApiOperation({ summary: '获取汽车信息' })
  @Get()
  async findAll(@Query() findAllCarInfoDto: FindAllCarInfoDto) {
    const result = await this.carInfoService.findAll(findAllCarInfoDto);
    return ApiResult.SUCCESS(result, 0, '获取成功！');
  }

  @ApiOperation({ summary: '根据id获取汽车信息' })
  @Get(':id')
  async findOne(@Param('id') id: string): Promise<ApiResult> {
    const result = await this.carInfoService.findOne(+id);
    return ApiResult.SUCCESS(result ?? [], 0, '获取成功！');
  }

  @ApiOperation({ summary: '根据id修改汽车信息' })
  @Patch(':id')
  async update(
    @Param('id') id: string,
    @Body() updateCarInfoDto: UpdateCarInfoDto,
  ): Promise<ApiResult> {
    const result = await this.carInfoService.update(+id, updateCarInfoDto);
    return ApiResult.SUCCESS(result, 0, '修改成功!');
  }

  @ApiOperation({ summary: '根据id删除汽车信息' })
  @Delete(':id')
  async remove(@Param('id') id: string): Promise<ApiResult> {
    const result = await this.carInfoService.remove(+id);
    if (result.affected >= 1) {
      return ApiResult.SUCCESS([], 0, '删除成功!');
    } else {
      return ApiResult.ERROR(-1, '删除失败!');
    }
  }
}
